From: Florian Müllner Date: Fri, 23 May 2025 20:34:14 +0000 (+0200) Subject: fileUtils: Handle symlinks when moving/deleting dirs X-Git-Tag: archive/raspbian/50.5-1+rpi1~1^2~124^2^2~88 X-Git-Url: https://dgit.raspbian.org/%22http://www.example.com/cgi/%22/%22http:/www.example.com/cgi/%22?a=commitdiff_plain;h=8d6e884bab3806484b17fe4b644721ccf9fde84b;p=gnome-shell.git fileUtils: Handle symlinks when moving/deleting dirs We can handle them like regular files for the operations in question, but we have to do so explicitly. Otherwise the link will not be moved or removed, and in the latter case prevent the parent directory from being removed as well (as it ends up non-empty). Closes: https://gitlab.gnome.org/GNOME/gnome-shell/-/issues/8417 Part-of: --- diff --git a/js/misc/fileUtils.js b/js/misc/fileUtils.js index cbbeac85e..216fa3e83 100644 --- a/js/misc/fileUtils.js +++ b/js/misc/fileUtils.js @@ -52,7 +52,7 @@ export function recursivelyDeleteDir(dir, deleteParent) { while ((info = children.next_file(null)) != null) { let type = info.get_file_type(); let child = dir.get_child(info.get_name()); - if (type === Gio.FileType.REGULAR) + if (type === Gio.FileType.REGULAR || type === Gio.FileType.SYMBOLIC_LINK) child.delete(null); else if (type === Gio.FileType.DIRECTORY) recursivelyDeleteDir(child, true); @@ -78,7 +78,7 @@ export function recursivelyMoveDir(srcDir, destDir) { let type = info.get_file_type(); let srcChild = srcDir.get_child(info.get_name()); let destChild = destDir.get_child(info.get_name()); - if (type === Gio.FileType.REGULAR) + if (type === Gio.FileType.REGULAR || type === Gio.FileType.SYMBOLIC_LINK) srcChild.move(destChild, Gio.FileCopyFlags.NONE, null, null); else if (type === Gio.FileType.DIRECTORY) recursivelyMoveDir(srcChild, destChild); diff --git a/tests/unit/fileUtils.js b/tests/unit/fileUtils.js index 47d881454..5aba0457b 100644 --- a/tests/unit/fileUtils.js +++ b/tests/unit/fileUtils.js @@ -77,6 +77,16 @@ describe('recursivelyDeleteDir()', () => { expect(() => recursivelyDeleteDir(dir, true)).not.toThrow(); expect(dir.query_exists(null)).toBeFalse(); }); + + it('handles symlinks', () => { + touchFile(dir, 'file'); + + const link = dir.get_child('link'); + link.make_symbolic_link('file', null); + + expect(() => recursivelyDeleteDir(dir, true)).not.toThrow(); + expect(dir.query_exists(null)).toBeFalse(); + }); }); describe('recursivelyMoveDir()', () => { @@ -123,4 +133,15 @@ describe('recursivelyMoveDir()', () => { expect(dstSubdir.query_exists(null)).toBeTrue(); expect(dstSubdir.get_child('file').query_exists(null)).toBeTrue(); }); + + it('handles symlinks', () => { + touchFile(srcDir, 'file'); + + const link = srcDir.get_child('link'); + link.make_symbolic_link('file', null); + + expect(() => recursivelyMoveDir(srcDir, dstDir)).not.toThrow(); + expect(dstDir.query_exists(null)).toBeTrue(); + expect(dstDir.get_child('link').query_exists(null)).toBeTrue(); + }); });